//
//  CZNetworkTools.swift
//  Swift网络请求单例类封装
//
//  Created by apple on 16/4/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
// 请求方式枚举
enum CZRequestMethod: Int{
    case GET = 0
    case POST = 1
}

class CZNetworkTools: AFHTTPSessionManager {
    
    // 网络请求全局访问点
    static let sharedTools:CZNetworkTools = {
        let tools = CZNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    
    
    // 网络请求方法
    func request(method:CZRequestMethod, urlString: String, params:AnyObject?,callback:(response: AnyObject?, error:NSError?)->() ){
        
//        let manager = AFHTTPSessionManager()
//        manager.responseSerializer.acceptableContentTypes?.insert("text/html")
        // GET
        if method == .GET{
            self.GET(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                callback(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callback(response: nil, error: error)
                    
            })
          // POST
        }else{
            self.POST(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                callback(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callback(response: nil, error: error)
                    
            })
        }
        
    }
    
    

}
extension CZNetworkTools{
    func loadHomeData(accessToken: String,callback:(response: AnyObject?, error:NSError?)->()){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["access_token":accessToken]
        request( CZRequestMethod.GET, urlString: urlString, params: params) { (response, error) -> () in
            callback(response: response, error: error)
        }
    }
}
